package drone.basis.util ;

import java.util.Objects ;

import javax.servlet.http.Cookie ;
import javax.servlet.http.HttpServletRequest ;
import javax.servlet.http.HttpServletResponse ;

public class CookieUtil
{

	public static void set (
			HttpServletResponse response ,
			String name ,
			String value ,
			int maxAge
	)
	{
		Cookie cookie = new Cookie ( name , value ) ; // 设置cookie的key和value值
		cookie.setPath ( "/" ) ; // 路径
		cookie.setMaxAge ( maxAge ) ; // 过期时间
		response.addCookie ( cookie ) ; // 添加cookie
	}

	public static String get (
			HttpServletRequest request ,
			String name
	)
	{
		Cookie [ ] cookies = request.getCookies ( ) ;
		if ( ! Objects.isNull ( cookies ) )
		{
			for ( Cookie cookie : cookies )
			{
				if ( cookie.getName ( ).equals ( name ) )
				{
					return cookie.getValue ( ) ;
				}
			}
		}
		return null ;
	}

}
